package com.woniuxy.happylife.suggest.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qiniu.common.QiniuException;
import com.woniuxy.happylife.entity.*;
import com.woniuxy.happylife.suggest.config.MultipartFileToFile;
import com.woniuxy.happylife.suggest.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author Fu
 * @Date 2022-02-15 11:54
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("suggestion")
public class SuggestionController {
    @Resource
    SuggestionService suggestionService;
    @Resource
    UserService userService;
    @Resource
    ReplyService replyService;
    @Resource
    TypeService typeService;
    @Resource
    IQiniuService qiniuService;

    @RequestMapping("add")
    public ResponseEntity add(MultipartFile file, @Validated Suggests suggests, BindingResult bindingResult) throws Exception {

        System.out.println(StringUtils.isEmpty(suggests.getContent()));
        if(bindingResult.hasErrors()){
            return new ResponseEntity(400, "添加失败", null);
        }
        Date date = new Date();
        suggests.setSubmitTime(date);
        log.info(suggests.getContent());
        File file1 = MultipartFileToFile.multipartFileToFile(file);

        String result = qiniuService.uploadFile(file1, file.getOriginalFilename()+date.getTime());
        suggests.setImageName(file.getOriginalFilename()+date.getTime());
        suggests.setUrl(result);
        suggestionService.insert(suggests);
        return new ResponseEntity(200, "添加成功", null);

    }
    @RequestMapping("test")
    public ResponseEntity test(@RequestParam("formData") MultipartFile[] files) throws Exception {
        for (MultipartFile file : files) {
            System.out.println(file.getOriginalFilename());
        }
        System.out.println("aaa");
        return new ResponseEntity(200, "添加成功", null);

    }

    @RequestMapping("findByCondition")
    public ResponseEntity findAll(@RequestBody FormInline formInline) {
        PageHelper.startPage(formInline.getPage(), formInline.getLimit());
        List<Suggests> list = suggestionService.findByCondition(formInline);
        PageInfo<Suggests> pageInfo = new PageInfo<>(list);
        return new ResponseEntity(200, "查询成功", pageInfo);
    }

    @RequestMapping("findById/{userId}")
    public ResponseEntity findById(@PathVariable("userId") Integer userId) {
        log.info(userId + " test");
        User user = userService.findById(userId);
        return new ResponseEntity(200, "查询成功", user);
    }

    @RequestMapping("addReply")
    public ResponseEntity addReply(@RequestBody Reply reply) {
        Date date = new Date();
        reply.setCreateTime(date);
        int i = replyService.insert(reply);
        Suggests suggests = new Suggests();
        suggests.setReplyPeople(reply.getReplyPeople());
        suggests.setFinishTime(date);
        suggests.setSuggestsId(reply.getSuggestsId());
        int j = suggestionService.updateStatus(suggests);
        return new ResponseEntity(200, "添加成功", null);

    }

    @RequestMapping("findReply/{suggestsId}")
    public ResponseEntity findReply(@PathVariable("suggestsId") Integer suggestsId) {
        Reply reply = replyService.findReply(suggestsId);
        log.info(reply.getUserId() + "测试");
        User user = userService.findById((int) reply.getUserId());

        List<Object> list = new ArrayList<>();
        list.add(reply);
        list.add(user);
        return new ResponseEntity(200, "成功", list);
    }

    @RequestMapping("findAllType")
    public ResponseEntity findAllType() {
        List<Type> list = typeService.findAllType();
        return new ResponseEntity(200, "success", list);
    }

    @RequestMapping("deleteById/{suggestsId}/{imageName}")
    public ResponseEntity deleteById(
            @PathVariable("suggestsId") Integer suggestsId, @PathVariable("imageName") String imageName) throws QiniuException {
        log.info(imageName);
        log.info(suggestsId.toString());
        //qiniuService.delete(imageName);
        int i = suggestionService.deleteById(suggestsId);
        //int j = replyService.deleteById(suggestsId);
        return new ResponseEntity(200, "删除成功", null);
    }
}
